package logic

import (
	"context"
	"shopping/shoppingapi/internal/svc"
	"shopping/shoppingapi/internal/types"
	"shopping/shoppingrpc/shoppingrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductsAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewProductsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductsAddLogic {
	return &ProductsAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ProductsAddLogic) ProductsAdd(req *types.ProductsAddRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	add, err := l.svcCtx.ShoppingRpc.ProductsAdd(l.ctx, &shoppingrpc.ProductsAddRequest{
		Name:        req.Name,
		Price:       float32(req.Price),
		Img:         req.Img,
		Description: req.Description,
		UserId:      req.UserId,
	})
	if err != nil {
		return &types.Response{
			Message: "商品发布失败",
			Code:    "500",
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Message: "商品发布成功",
		Code:    "200",
		Data:    add,
	}, nil
}
